<?php
/**
 * 数据验证类
 * @author jiangyong<jiangyong.go@qq.com>
 * @date   2025/5/20 14:38
 */

namespace App\Http\Controllers;

use App\Http\Requests\Form;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Spatie\RouteAttributes\Attributes\Any;
use Spatie\RouteAttributes\Attributes\Middleware;
use Spatie\RouteAttributes\Attributes\Prefix;
use Illuminate\Http\Request;

#[Prefix('validate')]
class ValidateController extends Controller
{
    public $user;
    public function __construct()
    {
        //$this->middleware(['web','auth']);
        $this->middleware(function ($request, $next) {
            //$request->user = Auth::user();
            //$request->user = User::find(1);
            return $next($request);
        });
    }

    /**
     * 指定header头 Accept application/json 才会返回json，不然会跳转到首页，如果存在上一页面，会跳转到上一页面
     * 验证 list[{'id'},{'id'}] 验证二位数组中的id字段
     * 多个错误 message 返回第一条错误信息 + '(and 1 more error)' errors 返回 多条记录 ['param1' => ['msg1', 'msg2'], 'param2' => ['msg1', 'msg2']]
     */
    #[Any('/')]
    public function index(request $request)
    {

        $rules = ['id' => ['required', 'distinct', 'string'],
            'name' => ['required', 'distinct', 'string'],];
        //$rules = ['list.*.id' => ['required', 'distinct', Rule::exists('lamp_user', 'id')],];
        $this->validate($request, $rules, [], ['list.*.id' => '用户ID']);
        return $request->all();
    }


    function receive(Request $request)
    {
        DebugBar::disable();
        $request->validate([
            'name' => 'required|min:2|max:10',
            'pswd' => 'required|min:6',
        ]);
        $data = $request->all();

        return $data;
    }


    function checked(Form $request)
    {
        DebugBar::disable();

        //获取已验证的数据
        $data = $request->validated();

        return $data;
    }
}
